﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="">
//   
// </copyright>
// <summary>
//   The string extension.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Core
{
    using System;

    /// <summary>
    ///   The string extension.
    /// </summary>
    public static class StringExtension
    {
        #region Public Methods and Operators

        /// <summary>
        /// The substring before.
        /// </summary>
        /// <param name="str">
        /// The str. 
        /// </param>
        /// <param name="pattern">
        /// The pattern. 
        /// </param>
        /// <param name="stringComparison">
        /// The string comparison. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        public static string SubstringBefore(
            this string str, string pattern, StringComparison stringComparison = StringComparison.CurrentCulture)
        {
            var index = str.IndexOf(pattern, StringComparison.OrdinalIgnoreCase);
            if (index != -1)
            {
                str = str.Substring(0, index);
            }

            return str;
        }

        /// <summary>
        /// The substring before.
        /// </summary>
        /// <param name="str">
        /// The str. 
        /// </param>
        /// <param name="ch">
        /// The ch. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        public static string SubstringBefore(this string str, char ch)
        {
            var index = str.IndexOf(ch);
            if (index != -1)
            {
                str = str.Substring(0, index);
            }

            return str;
        }

        #endregion
    }
}